﻿using daonaite.DAL.system;
using daonaite.Model.system.menu;
using daonaite.services.system;
using Dm.filter.log;
using Dm.filter.rw;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Drawing;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace daonaite.api.Controllers.system
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class MenuController : BaseController
    {
        private readonly menuService menucz;

        public MenuController(menuService menucz)
        {
            this.menucz = menucz;
        }

        /// <summary>
        /// 获取角色菜单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Getrolemenu()
        {
            var logInfo = getloginuser();
            if (logInfo.user.roleIds == null)
            {
                return Error("无角色信息");
            }
            var srolids = logInfo.user.roleIds.Split(",");
            var ids = Array.ConvertAll<string, int>(srolids, s => int.Parse(s));

            var treemenu = await menucz.getrolemenuAsync(ids);
            if (treemenu == null)
            {
                return Error("无菜单信息");
            }
            return Success(treemenu);
        }

        /// <summary>
        /// 获取树形菜单
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Gettreemenu([FromBody] Dto_menu data)
        {
            var treemenu = await menucz.getmenuAsync(data);
            return Success(treemenu);
        }

        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> add([FromBody] Dto_menu data)
        {
            var list = await menucz.selectAsync(new Dto_menu() { path = data.path });
            if (list.Count > 0)
            {
                return Error("路由已存在");
            }
            sys_menu menu = new sys_menu();
            copy<Dto_menu, sys_menu>(data, menu, true);
            if (menu.type == null)
            {
                menu.type = "M";
            }

            menu.ableFlag = "Y";
            int count = await menucz.add(menu);
            return Success(count);
        }

        /// <summary>
        /// 根据id查询菜单信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> get(int id)
        {
            var result = await menucz.getAsync(id);
            return Success(result);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> update([FromBody] sys_menu data)
        {
            sys_menu menu = new sys_menu();
            copy<sys_menu, sys_menu>(data, menu, false, true);
            int count = await menucz.updateAsync(menu);
            return Success(count);

        }

        /// <summary>
        /// 获取菜单与按钮
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> getmenuandbutton()
        {
            var result = await menucz.getmenuandbuttonAsync();
            return Success(result);
        }

        /// <summary>
        /// 查询菜单按钮
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> selectButton(int id)
        {
            var result = await menucz.selectButtonAsync(id);
            return Success(result);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> delete(int id)
        {
            var menu = await menucz.getAsync(id);
            if (menu.type == "B")
            {
                var count = await menucz.delete(id);
                return Success(count);
            }
            else
            {
                //检查是否存在子菜单

                var list = await menucz.selectAsync(new Dto_menu() { fid = id });
                if (list.Count == 0)
                {
                    var count = await menucz.delete(id);
                    return Success(count);
                }
                else
                {
                    return Error($"{menu.menuName}存在子菜单,请先删除子菜单");
                }

            }
        }

        /// <summary>
        /// 一键添加按钮
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> onkeyadd(int id)
        {
            var btns = await menucz.selectButtonAsync(id);
            if (btns.Count > 0)
            {
                return Error("已存在按钮,无法一键添加");
            }

            await menucz.onkeyadd(id);
            return Success();
        }
    }
}
